
package com.be.beadmin.device.impl;

import com.be.beadmin.device.entity.InspectStandard;
import com.be.beadmin.device.entity.InspectStandard_;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.service.dto.output.DictDataEx;
import com.be.beadmin.service.entity.DictType;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.device.mapper.InspectStandardMapperEx;
import com.be.beadmin.device.repository.InspectStandardRepository;
import com.be.beadmin.device.service.InspectStandardService;
import com.be.beadmin.device.dto.input.InspectStandardDto;
import com.be.beadmin.device.dto.output.InspectStandardEx;
import com.be.beadmin.device.dto.input.InspectStandardQueryCriteria;
import com.be.beadmin.device.mapper.InspectStandardMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.*;

/**
* @description 服务实现
* @author zhouzhou
* @date 2023-08-06
**/
@Service
@RequiredArgsConstructor
public class InspectStandardServiceImpl implements InspectStandardService {

    @Autowired
    private  InspectStandardRepository inspectStandardRepository;
    @Autowired
    private  InspectStandardMapper inspectStandardMapper;
    @Autowired
    private  InspectStandardMapperEx inspectStandardExMapper;

    @Override
    public Page<InspectStandardEx> getInspectStandardByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<InspectStandard> page = inspectStandardRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(InspectStandard_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(InspectStandard_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<InspectStandardEx> page1 = page.map(inspectStandardExMapper::toEntity);
            return page1;

    }

    @Override
    public void addInspectStandard(InspectStandardDto inspectStandardDto) {
        InspectStandard result = inspectStandardRepository.findInspectStandardByIdAndDelete(inspectStandardDto.getId(), 0);
        if(ObjectUtil.isNotEmpty(result)) {
            throw new BeadminException("InspectStandard不存在,请重新查询");
        }
        InspectStandard inspectStandard = inspectStandardMapper.toEntity(inspectStandardDto);
        inspectStandard.setTemplateId(inspectStandardDto.getTemplateId());  //use a smart tip
        inspectStandardRepository.save(inspectStandard);
    }

    @Override
    public void updateInspectStandard(InspectStandardDto inspectStandardDto) {
            InspectStandard inspectStandard = inspectStandardRepository.findInspectStandardById(inspectStandardDto.getId());
            inspectStandard = inspectStandardMapper.toEntity(inspectStandardDto);
            inspectStandardRepository.save(inspectStandard);
    }


    @Override
    public void delInspectStandard(String id) {
        InspectStandard inspectStandard = inspectStandardRepository.findInspectStandardById(id);
        inspectStandard.setDelete(1);
        inspectStandardRepository.save(inspectStandard);
    }

    @Override
    public void delInspectStandards (List<String> ids) { }

    @Override
    public List<InspectStandard> getInspectStandardByTemplateID(String TemplateID){
        List<InspectStandard> InspectStandardLists = inspectStandardRepository.findInspectStandardsByTemplateId(TemplateID);
        if(InspectStandardLists == null || InspectStandardLists.isEmpty())
            throw new BeadminException("未能根据模板ID找到模板");
        return InspectStandardLists;
    }


    @Override
    public List<String> getTemplateId() {
        List<String> productTypeList  = inspectStandardRepository.findTemplateIds();
        return productTypeList;
    }

    @Override
    public List<String> getCheckItem() {
        List<String> checkItems = inspectStandardRepository.findCheckItems();
        return checkItems;
    }

    @Override
    public String getCheckDescByCheckItem(String CheckItem) {
        String checkDescs = inspectStandardRepository.findCheckDescsByCheckItems(CheckItem);
        return checkDescs;
    }

}